package com.yuntsg.ruiijn.paperana.test;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.alibaba.fastjson.JSONArray;
import com.yuntsg.ruiijn.paperana.utils.SmallTool;
import common.extractText.Tools;
import common.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.yuntsg.ruiijn.paperana.test.ElasticsearchSaveDataText.dealManualDataStaNew;


@Slf4j
public class 测试新数据入库es {
    public static void main(String[] args) throws SQLException {
        // 2024年7月29日14:38:04 新版本入库es 进行查询
        // 2024年8月3日16:46:41 此版本是测试的 正式的入库在temp_project_es项目里面
        System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "6");
        SmallTool.printMemory();

        // 创建低级客户端
        RestClient restClient = RestClient
                .builder(new HttpHost("127.0.0.1", 9200))
                .setRequestConfigCallback(re -> {
                    re.setConnectTimeout(10000);
                    re.setSocketTimeout(150000);
                    re.setConnectionRequestTimeout(20000);
                    return re;
                })
                .setHttpClientConfigCallback(re -> {
                    re.setMaxConnTotal(1000);
                    re.setMaxConnPerRoute(1000);
                    return re;
                })
                .build();

        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//        // 创建API客户端
        ElasticsearchClient client = new ElasticsearchClient(transport);
//        String esIndex = "text_str_prod_new";
        String esIndex = "text_str_prod_test";
        String txtInsRecord = "txt_ins_record.txt_ins";
        String pmutyear = "txt_ins_record.pmutyear";
        createEs(client, esIndex);

        // 预先加载年份ut对应的
        Map<String, Integer> pmutYear = new ConcurrentHashMap<>();
//        pmutYear.put("10809655", 2000);
//        pmutYear.put("11513899", 2001);
//        pmutYear.put("10878844", 2000);
        Db use = Db.use(DSFactory.get("server_taiyidata"));

        List<Entity> query = use.query("select table_name from information_schema.TABLES where TABLE_SCHEMA='antiplagiarism' and TABLE_TYPE = 'base table' AND table_name LIKE ('%_txt%')");
        SmallTool.printMess("真实入库表数量+" + query.size());
        SmallTool.printMemory();
        SmallTool.printMess("组合pmut年份数据+");
        addPmutYear(pmutyear, use, pmutYear);
        SmallTool.printMess("组合年份数据结束+pmutYear++size+" + pmutYear.size());
        SmallTool.printMemory();
        if (query.size() > 0) {
            query.parallelStream().forEach(entity -> {
                String tableName = entity.getStr("table_name");
                long ls = System.currentTimeMillis();
                SmallTool.printMess("tableName+" + tableName + "开始了++_0_^_0_++");
                log.info("tableName+" + tableName + "开始了++_0_^_0_++");
                List<Entity> query1;
                try {
                    query1 = use.query("select * from " + tableName + "  ");
                    SmallTool.printMessLongTimeTake("读表耗时++" + tableName + "++++", ls);
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                Integer tableEnd = -1;
                if (tableName.contains("ncbi_pdf_to_txt") || tableName.contains("wos_pdf_to_txt_")) {

                    Integer datatype = 0;
                    if (tableName.startsWith("ncbi")) {
                        datatype = 1;
                    } else {
                        datatype = 2;
                    }
                    // ncbi_pdf_to_txt_685
                    // wos_pdf_to_txt_374
                    String[] split = tableName.split("_");
                    try {
                        tableEnd = Integer.parseInt(split[split.length - 1]);
                    } catch (Exception e) {

                    }
                    SmallTool.printMess("end++" + tableEnd);
                    if (tableEnd.intValue() < 0) {
                        SmallTool.printMess("表名异常");
                        SmallTool.printMessError(tableName);
                        return;
                    }


                    for (Entity entity1 : query1) {
                        byte[] optimalContents = entity1.getBytes("optimal_content");
                        String optimal_content = null;
                        String pmid_ut = null;
                        Integer id = null;
                        if (optimalContents != null) {
                            optimal_content = ZipUtil.unGzip(entity1.getBytes("optimal_content"), "utf-8");
                            pmid_ut = entity1.getStr("pmid_ut");
                            id = entity1.getInt("id");
                        }
                        boolean bins = false;

                        try {
                            bins = checkIsIns(use, txtInsRecord, id);
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        if (bins) {
                            int realSize = 0;
//                            SmallTool.printMess("pmid+" + pmid_ut);
                            if (StringUtil.isNotNull(optimal_content) && StringUtil.isNotNull(pmid_ut) && StringUtil.isNotNull(id)) {
                                if (StringUtil.isNotNull(optimal_content)) {
                                    List<EsInsertNew> esInsertNewList = new ArrayList<>(1000000);
                                    // 进行es 入库测试
                                    //  目前查看了代码 只用到了 如下
                                    // file_id
                                    // table_id
                                    // str
                                    // pmid_ut
                                    List<TableEntitySplit> dataParseEntityList = JSONArray.parseArray(optimal_content, TableEntitySplit.class);
                                    for (TableEntitySplit tableEntitySplit : dataParseEntityList) {
                                        String str = tableEntitySplit.getStr();
                                        boolean b = checkIsCorrect(str);
                                        if (b) {
                                            EsInsertNew insertNew = new EsInsertNew();
                                            insertNew.setParagraph_name(tableEntitySplit.getParagraph_name());
                                            insertNew.setCode(tableEntitySplit.getCode());
                                            insertNew.setParagraph(tableEntitySplit.getParagraph());
                                            if (pmutYear.containsKey(pmid_ut)) {
                                                insertNew.setYear(pmutYear.get(pmid_ut));
                                            } else {
                                                insertNew.setYear(9999);
                                            }
                                            insertNew.setSentence(tableEntitySplit.getSentence());
                                            insertNew.setSub_project(tableEntitySplit.getSub_project());
                                            insertNew.setDatabase_type(datatype);
                                            insertNew.setStr(tableEntitySplit.getStr());
                                            insertNew.setPmid_ut(pmid_ut);
                                            insertNew.setFile_id(id);
                                            insertNew.setTable_id(tableEnd);
                                            esInsertNewList.add(insertNew);
                                            realSize++;
                                        }

                                    }
                                    boolean b = dealManualDataStaNew(esInsertNewList, client, esIndex);
//                                System.out.println("入库情况++" + b);
                                    Entity insTxtRe = new Entity();
                                    insTxtRe.setTableName(txtInsRecord);
                                    insTxtRe.set("tab_name", tableName);
                                    insTxtRe.set("tab_id", id);
                                    insTxtRe.set("si", realSize);
                                    try {
                                        use.insert(insTxtRe);
                                    } catch (SQLException e) {
                                        throw new RuntimeException(e);
                                    }
                                }

                            }
                        } else {
//                            SmallTool.printMess("已入库略过");
                        }

                    }
                }
                SmallTool.printMessLongTimeTake("table 入库结束+" + tableName, ls);
                log.info("table+" + tableName + "入库结束");

            });
        }
    }

    private static boolean checkIsIns(Db use, String tableName, Integer id) throws SQLException {
        Entity entity = use.queryOne("select * from " + tableName + " where tab_id = ? ", id);
        if (entity != null) {
            return false;
        } else {
            return true;
        }


    }

    public static void createEs(ElasticsearchClient client, String index) {
        // 进行创建es 数据库和
        ElasticsearchSaveDataText.createIndexNew(client, index);
    }

    public static boolean checkIsCorrect(String s) {
        boolean ISC = true;
        if (StringUtil.isNull(s)) {
            return !ISC;
        } else {
            String[] split = s.split(" ");
            if (split.length <= 4) {
                return !ISC;
            } else {
                return ISC;
            }
        }
    }

    public static void addPmutYear(String tab, Db use, Map<String, Integer> pmutYear) {
        Tools.getAllDbInfo(use, tab, "id", 12, pmutYear);
    }

}
